package com.bw.finance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.finance.auth.UserInfo;
import com.bw.finance.conf.ResponseResult;
import com.bw.finance.eneity.TbUser;
import com.bw.finance.eneity.cms.EduWebsiteAd;
import com.bw.finance.eneity.cms.EduWebsiteBase;
import com.bw.finance.service.IEduWebsiteBaseService;
import com.bw.finance.utils.MD5Utils;
import com.bw.finance.utils.PhotoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zxx
 * @since 2020-08-16
 */
@RestController
@RequestMapping("/wbbase")
public class EduWebsiteBaseController {
    @Autowired
    private IEduWebsiteBaseService baseService;

    private static final Logger log= LoggerFactory.getLogger(EduWebsiteBaseController.class);

    @GetMapping("/findwbbase")
    public ResponseResult findwbbase(){
        log.info("网站基本信息查询开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper=new QueryWrapper();
            EduWebsiteBase websiteBase = baseService.getOne(queryWrapper);
            result.setResult(websiteBase);
        }catch (Exception e){
            log.error("网站基本信息查询异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("网站基本信息查询异常"+e.toString());
        }
        log.info("网站基本信息查询结束");
        return result;
    }

    @PostMapping("/addwbbase")
    public ResponseResult findwbbase(@RequestBody EduWebsiteBase websiteBase){
        log.info("网站基本信息修改开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            baseService.saveOrUpdate(websiteBase);
            result.setMessage("保存成功！！！");
        }catch (Exception e){
            log.error("网站基本信息修改异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("网站基本信息修改异常"+e.toString());
        }
        log.info("网站基本信息修改结束");
        return result;
    }

    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile file){
        log.info("网站logo上传开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            String path = PhotoUtils.upload(file);
            Map map=new HashMap();
            map.put("path",path);
            result.setResult(map);
        }catch (Exception e){
            log.error("网站logo上传异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("上传异常"+e.toString());
        }
        log.info("网站logo上传结束");
        return result;
    }

    @GetMapping("/download/{path}")
    public ResponseResult download(@PathVariable String path,HttpServletResponse response){
        log.info("网站logo下载开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            PhotoUtils.download(path,response);
        }catch (Exception e){
            log.error("网站logo下载下载异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("异常"+e.toString());
        }
        log.info("网站logo下载结束");
        return result;
    }

    @GetMapping("/getlogo")
    public List<EduWebsiteBase> getlogo(){
        log.info("网站基本信息数据获取开始");
        ResponseResult result = ResponseResult.SUCCESS();
        List<EduWebsiteBase> list=new ArrayList<>();
        try {
            list = baseService.list();
        }catch (Exception e){
            log.error("网站基本信息数据获取异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("网站基本信息数据获取异常"+e.toString());
        }
        log.info("网站基本信息数据获取结束");
        return list;
    }

    @GetMapping("/getwbinfo")
    public List<EduWebsiteBase> getwbinfo(){
        log.info("网站基本信息数据获取开始");
        ResponseResult result = ResponseResult.SUCCESS();
        List<EduWebsiteBase> list=new ArrayList<>();
        try {
            list = baseService.list();
        }catch (Exception e){
            log.error("网站基本信息数据获取异常"+e.toString());
            result.setSuccess(false);
            result.setMessage("网站基本信息数据获取异常"+e.toString());
        }
        log.info("网站基本信息数据获取结束");
        return list;
    }
}
